/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ABMListasPrecios.java
 *
 * Created on 28-ago-2009, 9:19:18
 */
package ar.com.jpack.desktop.ventas;

import ar.com.jpack.desktop.DesktopApp;
import ar.com.jpack.helpers.CustomInternalFrame;
import ar.com.jpack.helpers.CustomTableModelListener;
import ar.com.jpack.helpers.tablemodels.ListasPreciosTableModel;
import ar.com.jpack.transferencia.ListasPreciosT;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import org.jdesktop.application.Action;

/**
 *
 * @author Pablo
 */
public class ABMListasPrecios extends CustomInternalFrame<ListasPreciosT> {

    /** Creates new form ABMListasPrecios */
    public ABMListasPrecios() {
        super(new ListasPreciosT());
        initComponents();
        HashMap parametros = new HashMap();
        parametros.put("pJoinEstados", true);
        parametros.put("pJoinUsuarios", true);
        List<ListasPreciosT> nuevo = DesktopApp.getApplication().getListasPreciosT(parametros);
        setListDto((ArrayList<ListasPreciosT>) nuevo);

        tableModel = new ListasPreciosTableModel(columnNames, this.getListDto());
        tableModel.addTableModelListener(new CustomTableModelListener());
        tblListasPrecios.setModel(tableModel);

        sorter = new TableRowSorter<TableModel>(tableModel) {

            @Override
            public void toggleSortOrder(int column) {
                RowFilter<? super TableModel, ? super Integer> f = getRowFilter();
                setRowFilter(null);
                super.toggleSortOrder(column);
                setRowFilter(f);
            }
        };
        tblListasPrecios.setRowSorter(sorter);

        setModificado(false);
        setNuevo(false);


        parametros = new HashMap();

        tblListasPrecios.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }

    @Action
    public void agregar() {

        if (JOptionPane.showInternalConfirmDialog(this, "Se generara una nueva lista de precios y perdera vigencia la actual\n¿Esta seguro?", "Alerta", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            String porcentajeString = (String) JOptionPane.showInternalInputDialog(this,
                    "Porcentaje de ajuste de la nueva lista de precio" +
                    "\n\nPorcentaje",
                    "Porcentaje de ajuste de la nueva lista de precio",
                    JOptionPane.PLAIN_MESSAGE);
            Double porcentaje = new Double("0");
            try {
                porcentaje = Double.valueOf(porcentajeString);
            } catch (Exception e) {
                JOptionPane.showInternalMessageDialog(this, "El porcentaje debe ser numerico");
            }

            DesktopApp.getApplication().insertarListasPreciosT(porcentaje, DesktopApp.getApplication().getUsuarioLogueado());

            HashMap parametros = new HashMap();
            parametros.put("pJoinEstados", true);
            parametros.put("pJoinUsuarios", true);
            List<ListasPreciosT> nuevo = DesktopApp.getApplication().getListasPreciosT(parametros);
            setListDto((ArrayList<ListasPreciosT>) nuevo);

            tableModel = new ListasPreciosTableModel(columnNames, this.getListDto());
            tableModel.addTableModelListener(new CustomTableModelListener());
            tblListasPrecios.setModel(tableModel);

            sorter = new TableRowSorter<TableModel>(tableModel) {

                @Override
                public void toggleSortOrder(int column) {
                    RowFilter<? super TableModel, ? super Integer> f = getRowFilter();
                    setRowFilter(null);
                    super.toggleSortOrder(column);
                    setRowFilter(f);
                }
            };
            tblListasPrecios.setRowSorter(sorter);
        }
    }

    @Action
    public void cancelar() {
        try {
            this.setClosed(true);
        } catch (PropertyVetoException ex) {
            Logger.getLogger(ABMListasPrecios.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblListasPrecios = new javax.swing.JTable();
        btnCancelar = new javax.swing.JButton();
        btnAgregar = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setName("Form"); // NOI18N
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tblListasPrecios.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblListasPrecios.setName("tblListasPrecios"); // NOI18N
        jScrollPane1.setViewportView(tblListasPrecios);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(ar.com.jpack.desktop.DesktopApp.class).getContext().getActionMap(ABMListasPrecios.class, this);
        btnCancelar.setAction(actionMap.get("cancelar")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ar.com.jpack.desktop.DesktopApp.class).getContext().getResourceMap(ABMListasPrecios.class);
        btnCancelar.setText(resourceMap.getString("btnCancelar.text")); // NOI18N
        btnCancelar.setName("btnCancelar"); // NOI18N

        btnAgregar.setAction(actionMap.get("agregar")); // NOI18N
        btnAgregar.setText(resourceMap.getString("btnAgregar.text")); // NOI18N
        btnAgregar.setName("btnAgregar"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(279, 279, 279)
                        .addComponent(btnAgregar, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnAgregar))
                .addGap(12, 12, 12))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        // TODO add your handling code here:
        if (isModificado() || isNuevo()) {
            if (JOptionPane.showInternalConfirmDialog(this, "Hay informacion que no han sido guardada\n¿Desea cerrar de todos modos?", "Alerta", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                dispose();
            }
        } else {
            dispose();
        }
    }//GEN-LAST:event_formInternalFrameClosing
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblListasPrecios;
    // End of variables declaration//GEN-END:variables
    public static final String[] columnNames = {
        "Id", "Fecha Desde", "Fecha Hasta", "Estado", "Usuario", "Fecha Modificacion"
    };
    protected ListasPreciosTableModel tableModel;
    private TableRowSorter<TableModel> sorter;
}
